package demo6;


import java.time.temporal.Temporal;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class TreeNode {
    public TreeNode left;
    public TreeNode right;
    public int val;
    public TreeNode(int val) {
        this.val = val;
    }
}
public class MyTree {

    public TreeNode createTree() {
        TreeNode A = new TreeNode(1);
        TreeNode B = new TreeNode(56);
        TreeNode D = new TreeNode(18);
        TreeNode E = new TreeNode(23);
        TreeNode H = new TreeNode(33);
        TreeNode C = new TreeNode(12);
        TreeNode F = new TreeNode(45);
        TreeNode G = new TreeNode(8);
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        return A;
    }
    public void preorderTraversal(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preorderTraversal(root.left);
        preorderTraversal(root.right);
    }
    public List<Integer> preorderTraversal1(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        if(root == null) {
            return list;
        }
        list.add(root.val);
        List<Integer> leftTree = preorderTraversal1(root.left);
        if(leftTree != null) {
            list.addAll(leftTree);
        }
        List<Integer> rightTree = preorderTraversal1(root.right);
        if(rightTree != null) {
            list.addAll(rightTree);
        }
        return list;
    }


    //法一：
    public int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }
    //法二：遍历
    public static int count;
    public int size1(TreeNode root) {
        if(root == null) {
            return 0;
        }
        count++;
        size1(root.left);
        size1(root.right);
        return count;
    }
    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return Math.max(leftHeight,rightHeight) + 1;
    }

    public int getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    //第k层节点 = 左子树第k-1层节点 + 右子树第k-1层节点
    public int getKLevelNodeCount(TreeNode root, int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }

    public TreeNode getVal(TreeNode root, int val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode leftTree = getVal(root.left,val);
        if(leftTree != null) {
            return leftTree;
        }
        TreeNode rightTree = getVal(root.right,val);
        if(rightTree != null) {
            return rightTree;
        }
        return null;
    }

    public boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur == null) {
                break;
            }
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                return false;
            }
        }
        return true;
    }
    public String toString(TreeNode root) {
        return "root的val为" + root.val;
    }
}
